<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <!-- 引入Bootstrap CSS -->
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.1.1/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>

<div class="container mt-5">
    <h2 class="mb-4">注册</h2>
    <form id="registerForm">
        <div class="mb-3">
            <label for="username" class="form-label form-label-required">账号:</label>
            <input type="text" class="form-control" id="username" name="username" required>
            <span id="input1" style="color: red"></span>
        </div>
        <div class="mb-3">
            <label for="password" class="form-label form-label-required">密码:</label>
            <input type="password" class="form-control" id="password" name="password" required>
            <span id="input2" style="color: red"></span>
        </div>
        <div class="mb-3">
            <label for="confirmPassword" class="form-label form-label-required">确认密码:</label>
            <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required>
            <span id="input3" style="color: red"></span>
        </div>
        <button type="button" class="btn btn-primary" onclick="register()">注册</button>
        <button type="button" class="btn btn-secondary ms-2" onclick="resetForm()">重置表单</button>
        <div class="mt-3">
            <p>已有账号？<a href="login.html" class="text-decoration-none">去登录</a></p>
        </div>
    </form>
</div>

<!-- 引入Bootstrap Bundle JS (包含Popper) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    function register() {
        $("#input1").text("");
        $("#input2").text("");
        $("#input3").text("");
        var username=$("#username").val().trim();
        var password=$("#password").val().trim();
        var confirmPassword=$("#confirmPassword").val().trim();
        if (username==""){
            $("#input1").text("账号不能为空");
        }
        if (password==""){
            $("#input2").text("密码不能为空");
        }
        if (confirmPassword==""){
            $("#input3").text("二次密码不能为空");
        }else if (password!=confirmPassword){
            $("#input3").text("两次输入密码不一致");
        }
        var entity={
            username:username,
            password:password
        }
        $.ajax({
            url:"system/register",
            data:JSON.stringify(entity),
            type:"post",
            contentType: "application/json",
            success:function(msg){
                console.log(msg);
                if (msg==1){
                    alert("注册成功");
                    window.location.href="login.html";
                    return;
                }else if (msg==-1){
                    alert("账户已存在")
                    return;
                }else{
                    alert("注册失败");
                    return;
                }


            },
            dataType:"JSON"
        });

    }
    function resetForm() {
        // 使用Bootstrap的内置方法重置表单
        document.getElementById('registerForm').reset();
    }
</script>

</body>
</html>